package com.hyh.music.controller;

import com.hyh.music.Result.PageResult;
import com.hyh.music.Result.Result;
import com.hyh.music.domain.domainDTO.SingerDTO;
import com.hyh.music.domain.domainDTO.SingerPageDTO;
import com.hyh.music.domain.domains.Singer;
import com.hyh.music.service.SingerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    SingerService singerService;

    @GetMapping("/Page")
    public Result<PageResult> PageSelect(SingerPageDTO singerPageDTO){
        log.info("歌手分页查询{}",singerPageDTO);
        PageResult singers = singerService.PageSelect(singerPageDTO);
        return Result.success(singers);
    }

    @PostMapping("/addSinger")
    public Result addSinger(@RequestBody SingerDTO singerDTO){
        log.info("添加歌手{}",singerDTO);
        singerService.addSinger(singerDTO);
        return Result.success();
    }
    @DeleteMapping("/deleteSinger")
    public Result deleteSinger(Integer id){
        log.info("删除{}的歌手",id);
        Boolean aBoolean = singerService.deleteSinger(id);
        if(aBoolean){
            return Result.success();
        }
        else {
            return Result.error("歌手仍有歌曲不能删除");
        }

    }
    @GetMapping("/{id}")
    public Result<Singer> selectById( @PathVariable Integer id){
        log.info("查询id为{}的歌手",id);
        Singer singer = singerService.selectById(id);
        return Result.success(singer);
    }
    @PutMapping("/update")
    public Result UpdateById(@RequestBody Singer singer){
        log.info("更新{}的数据",singer.getName());

        singerService.Update(singer);

        return Result.success();
    }



}
